<?php
class SubForumsController extends AppController {
	public $name = 'SubForums';
	public $paginate = array();


	function beforeFilter() {
		$this->Auth->allow('displaytopics');
		$this->Auth->authorize = 'controller';
		$this->Security->blackHoleCallback = 'blackhole';
	}

	function blackhole($type) {
		if($this->request->params['action'] == 'updatePositions'){
			return true;
		}
		throw new MethodNotAllowedException(__('Nope: ' . $type . ' - ' . Router::url($this->here, true)));
	}

	public function isAuthorized($user) {
		if (in_array(strtolower($this->action), array('add', 'edit', 'delete', 'index', 'updatepositions'))) {
			return (AuthComponent::User('role') == '3') ? true : false;
		}
		return true;
	}


	public function index(){
		$this->set('roles', array(1 => 'Member', 2 => 'Moderator', 3 => 'Admin'));
		$this->set('subForums', $this->SubForum->find('all', array('order' => 'SubForum.position ASC')));
	}

	public function add(){
		$this->set('forums', $this->SubForum->Forum->find('list'));
		if($this->request->is('post') || $this->request->is('put')){
			$this->SubForum->Create();
			if($this->SubForum->saveAssociated($this->request->data)){
				$this->Session->setFlash('Sub forum created', 'default', array('class' => 'success'));
				$this->redirect(array('controller' => 'subforums', 'action' => 'index'));
			}
		}
	}

	public function edit($id = null){
		$this->SubForum->id = $id;
		if(!$this->SubForum->exists()){
			throw new NotFoundException('Sub forum not found');
		}
		$this->set('forums', $this->SubForum->Forum->find('list'));
		if($this->request->is('get')){
			$this->request->data = $this->SubForum->read();
		}else{
			if($this->SubForum->saveAssociated($this->request->data)){
				$this->Session->setFlash('Sub forum updated', 'default', array('class' => 'success'));
				$this->redirect(array('controller' => 'subforums', 'action' => 'index'));
			}
		}
	}

	public function displayTopics($sub_forum_id = null, $slug = null){
		$this->SubForum->id = $sub_forum_id;
		if(!$this->SubForum->exists()){
			throw new NotFoundException('Sub forum not found');
		}

		$this->paginate = array(
			'conditions' => array('Thread.sub_forum_id' => $sub_forum_id, 'Thread.thread_id' => '0'),
			'order' => array('Thread.sticky' =>  'DESC', 'Thread.latest_reply_thread_id' =>  'DESC'),
			'limit' => 15
		);

		$this->SubForum->recursive = -1;
		$subForum = $this->SubForum->read();

		$this->set('subForum', $subForum);
		$this->set('title_for_layout', 'Forums &bull; ' . $subForum['SubForum']['name']);
		$this->set('subForumName', $subForum['SubForum']['name']);
		$this->set('threads', $this->paginate('SubForum.Thread'));

		if(AuthComponent::User('role') == '3'){
			$this->render('admin_display_topics');
		}
	}

	public function updatePositions(){
		if($this->request->is('ajax')){
			$this->autoRender = false;

			foreach($this->request->data['subforum'] as $key=>$val){
				$this->SubForum->id = $val;
				$this->SubForum->set('position', $key);
				$this->SubForum->save();
			}
		}
	}


	public function delete(){
		if($this->request->is('get')){
			throw new MethodNotAllowedException();
		}elseif($this->request->is('post')){
			$i = 0;
			foreach($this->request->data['SubForum']['selectedSubForums'] AS $subForumId){
				// delete forum
				$this->SubForum->id = $subForumId;
				$this->SubForum->delete();
				$i++;
			}
			$this->Session->setFlash($i . ': Sub forums have been deleted.', 'default', array('class' => 'success'));
			$this->redirect(array('action' => 'index'));
		}
	}
}